// function Node(element) {
//   this.element = element
//   this.next  = next //指针指向下一个节点
// }
//
class Node {
  constructor(element) {
    this.element = element
    this.next = null
  }
}

class LinedList {
  constructor() {
    this.size = 0
    this.head = null
  }

  append(element) {
    let node = new Node(element)
    if(this.head === null) {
      this.head = node
    }else {
      let current = this.getNode(this.size - 1)
      current.next = node
    }
    this.size++
  }

  appendAt(position,element) {
     if(position < 0 || position > this.size) {
      throw new Error('position out range')
    }
    let node = new Node(element)
    if(position==0) {
      node.next = this.head
      this.head = node
    } else {
        let pre = this.getNode(position-1)
        node.next = pre.next
        pre.next = node
    }
    this.size++
  }

  remove(positoin) {

  }

  indexOf(element) {
    let current = this.head
    for(let i=0; i< this.size;i++) {
      if(current.element===element) {
        return  i
      }
      current = current.next
    }
    return -1
  }

  getNode(index) {
    if(index < 0 || index >= this.size) {
      throw new Error('out range')
    }
    let current = this.head
     for(let i =0;i < index; i++) {
        current = current.next
    }
    return current
  }
}

let ll = new LinedList()
ll.append(1)
ll.append(2)
ll.append(3)
ll.append(4)
ll.appendAt(0,0)
console.dir(ll,{
  depth: 100
})
